//
//  BLETask.h
//  BLESDK-BLESDK
//
//  Created by 李阳 on 2018/8/29.
//

#import <Foundation/Foundation.h>

/// 任务状态
typedef NS_ENUM(NSUInteger, BLETaskState)
{
    BLETaskStateRunning = 0,
    BLETaskStateSuspended,
    BLETaskStateCanceling,
    BLETaskStateCompleted
};

/// 任务优先等级
typedef NS_ENUM(NSUInteger, BLETaskPriority)
{
    BLETaskPriorityDefault, ///< 默认优先极
    BLETaskPriorityHigh, ///< 高优先级
    BLETaskPriorityLow ///< 低优先级
};

@class BLERequest;
@class BLEDevice;
@protocol BLESessionTaskDelegate;
@protocol BLESessionDelegate;

@interface BLETask : NSObject

/// 当前请求
@property (nonatomic, strong, readonly) BLERequest *request;

/// 任务标识
@property (nonatomic, assign, readonly) NSUInteger identifier;
/// 任务描述
@property (nullable, copy, readonly) NSString *description;

/// 任务等级
@property (nonatomic, assign) BLETaskPriority priority;

///< 已发送的数据大小
@property (nonatomic, assign, readonly) int64_t countOfBytesSent;
///< 期待发送的数据大小
@property (nonatomic, assign, readonly) int64_t countOfBytesExpectedToSend;
///< 已接收的数据大小
@property (nonatomic, assign, readonly) int64_t countOfBytesReceived;
///< 期待接收的数据大小
@property (nonatomic, assign, readonly) int64_t countOfBytesExpectedToReceive;

/// 任务状态
@property (nonatomic, assign, readonly) BLETaskState state;

/// 错误
@property (nonatomic, strong, readonly) NSError *error;

/// 完成的回调
@property (nonatomic, copy) void(^completionBlock)(__kindof BLETask *task);

/// 取消
- (void)cancel;
/// 挂起
- (void)suspend;
/// 开始
- (void)resume;

- (void)completeWithError:(NSError *)error;

@end


@interface BLENormalTask : BLETask
@end

@interface BLEMediaTask : BLETask
@end

@interface BLEContactTask : BLETask
@end


@interface BLESession : NSObject

/// 代理
@property (nonatomic, weak, readonly) id <BLESessionTaskDelegate> delgate;

/// 正在执行的普通蓝牙任务
@property (nonatomic, strong, readonly) BLETask *runningNormalTask;
/// 正在执行的媒体蓝牙任务
@property (nonatomic, strong, readonly) BLETask *runningMedialTask;
/// 正在执行的联系人蓝牙任务
@property (nonatomic, strong, readonly) BLETask *runningContactTask;

/// 创建task
- (BLETask *)taskWithRequest:(BLERequest *)request;
- (BLENormalTask *)normalTaskWithReuest:(BLERequest *)request;
- (BLEMediaTask *)mediaTaskWithReuest:(BLERequest *)request;
- (BLEContactTask *)contactTaskWithReuest:(BLERequest *)request;

/// 添加任务
- (void)addTask:(BLETask *)task;
- (void)addNormalTasks:(BLENormalTask *)task;
- (void)addMediaTasks:(BLEMediaTask *)task;
- (void)addContactTasks:(BLEContactTask *)task;


/// 开始任务
- (void)beginTasks;
- (void)beginNormalTasks;
- (void)beginMediaTasks;
- (void)beginContactTasks;

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks;


- (instancetype)initWithDelegate:(id <BLESessionDelegate>)delegate NS_DESIGNATED_INITIALIZER;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;

@end

@protocol BLESessionDelegate <NSObject>


@end

@protocol BLESessionTaskDelegate <BLESessionDelegate>

@required

- (void)session:(BLESession *)session
           normalTask:(BLENormalTask *)task
   beginRequest:(BLERequest *)request;

- (void)session:(BLESession *)session
     mediaTask:(BLEMediaTask *)task
   beginRequest:(BLERequest *)request;

- (void)session:(BLESession *)session
     contactTask:(BLEContactTask *)task
   beginRequest:(BLERequest *)request;

@end
